package com.zjfgz.lib_kt_com.mvp.baseui.fragment

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.zjfgz.lib_kt_com.mvp.baseui.activity.BaseActivity
import com.zjfgz.lib_kt_com.mvp.baseui.mvp.BaseMvpPresenter
import com.zjfgz.lib_kt_com.mvp.baseui.mvp.BaseMvpView
import com.zjfgz.lib_kt_com.mvp.baseui.utils.MvpUtils

abstract class BaseMFragment<V : BaseMvpView, P : BaseMvpPresenter<V>> :
    Fragment(), BaseMvpView {

    lateinit var mActivity: BaseActivity

    var presenter: P? = null

    lateinit var rootView: View

    private fun initMvp() {
        presenter = MvpUtils.getT<P>(this, 1)
        presenter!!.attchView(this as V)
    }

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        if (activity is BaseActivity)
            mActivity = activity
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        initMvp()
          rootView = inflater.inflate(getLayout(), container, false)
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }


    abstract fun getLayout():Int
    abstract fun initView()

    override fun onDestroyView() {
        if (presenter != null) {
            presenter!!.detachView()
            presenter = null
        }
        super.onDestroyView()
    }


    override fun isWaitDialogShow(): Boolean {
        return mActivity.isWaitDialogShow()
    }

    override fun getWaitDialog(): Dialog {
        return mActivity.getWaitDialog()
    }

    override fun getmFragment(): BaseFragment? {
        return null
    }

    override fun showWaitDialog() {
        mActivity.showWaitDialog()
    }

    override fun showWaitDialog(message: String) {
        mActivity.showWaitDialog(message)
    }

    override fun showWaitDialog(message: String, cancelable: Boolean) {
        mActivity.showWaitDialog(message,cancelable)
    }

    override fun getmActivity(): BaseActivity {
        return mActivity
    }

    override fun hideWaitDialog() {
        mActivity.hideWaitDialog()
    }

    override fun showToast(msg: String?) {
        mActivity.showToast(msg)
    }

    override fun showToast(strId: Int) {
        mActivity.showToast(strId)
    }

    override fun showStatusEmptyView(emptyMessage: String) {
        mActivity.showStatusEmptyView(emptyMessage)
    }

    override fun showStatusErrorView(emptyMessage: String) {
        mActivity.showStatusErrorView(emptyMessage)
    }

    override fun showStatusLoadingView(loadingMessage: String) {
        mActivity.showStatusLoadingView(loadingMessage)
    }

    override fun showStatusLoadingView(loadingMessage: String, isHasMinTime: Boolean) {
        mActivity.showStatusLoadingView(loadingMessage)
    }

    override fun hideStatusView() {
        mActivity.hideStatusView()
    }
}